package org.lucene;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.codecs.appending.AppendingCodec;
import org.apache.lucene.codecs.bloom.BloomFilteringPostingsFormat;
import org.apache.lucene.codecs.lucene3x.Lucene3xCodec;
import org.apache.lucene.codecs.lucene40.Lucene40Codec;
import org.apache.lucene.codecs.lucene40.Lucene40PostingsFormat;
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * lucene codec
 * 
 * @author wuwen
 * @date 2013-1-14 下午04:54:17
 * 
 */
public class LuceneCodecTest {

	static Codec getCodec(String codecname) {
		Codec codec = null;
		if ("Lucene40".equals(codecname)) {
			codec = new Lucene40Codec();
		} else if ("Lucene3x".equals(codecname)) {
			codec = new Lucene3xCodec();
//			throw new UnsupportedOperationException("this codec can only be used for reading");
		}
		else if ("SimpleText".equals(codecname)) {
			codec = new SimpleTextCodec();
		} else if ("Appending".equals(codecname)) {
			codec = new AppendingCodec();
		} else if ("Pulsing40".equals(codecname)) {
			 codec = new Lucene40Codec() {
			      public PostingsFormat getPostingsFormatForField(String field) {
			          return PostingsFormat.forName("Pulsing40");
			      }
			 };
		} else if ("Memory".equals(codecname)) {
			 codec = new Lucene40Codec() {
			      public PostingsFormat getPostingsFormatForField(String field) {
			          return PostingsFormat.forName("Memory");
			      }
			 };
		} else if ("BloomFilter".equals(codecname)) {
			 codec = new Lucene40Codec() {
			      public PostingsFormat getPostingsFormatForField(String field) {
			          return new BloomFilteringPostingsFormat(new Lucene40PostingsFormat());
			      }
			 };
		}else if ("Direct".equals(codecname)) {
			 codec = new Lucene40Codec() {
			      public PostingsFormat getPostingsFormatForField(String field) {
			          return PostingsFormat.forName("Direct");
			      }
			 };
		} else if ("Block".equals(codecname)) {
			 codec = new Lucene40Codec() {
			      public PostingsFormat getPostingsFormatForField(String field) {
			          return PostingsFormat.forName("Block");
			      }
			 };
		}
		return codec;
	}
	
	public static void main(String[] args) {
//		String[] codecs = {"Lucene40", "Lucene3x", "SimpleText", "Appending", "Pulsing40", "Memory", "BloomFilter", "Direct", "Block"};
		String[] codecs = {"Lucene40", "SimpleText", "Appending", "Pulsing40", "Memory", "BloomFilter", "Direct", "Block"};
		String suffixPath = "/lucene/codec/";
		for (String codecname : codecs) {
			String indexPath = suffixPath+codecname;
			Codec codec = getCodec(codecname);
			Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_40);
			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
			config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
			config.setCodec(codec);		// 设置编码器
			IndexWriter writer = null;
			try {
				Directory luceneDir = FSDirectory.open(new File(indexPath));
				writer = new IndexWriter(luceneDir, config);
				List<Document> list = new ArrayList<Document>();
				Document doc = new Document();

				doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
				doc.add(new TextField("Content", "北京时间1月14日04:00(西班牙当地时间13日21:00)，2012/13赛季西班牙足球甲级联赛第19轮一场焦点战在纳瓦拉国王球场展开争夺.", Field.Store.YES));
				list.add(doc);

				doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
				doc.add(new TextField("Content", "巴萨超皇马18分毁了西甲？媒体惊呼 克鲁伊夫看不下去.", Field.Store.YES));
				list.add(doc);

				doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
				doc.add(new TextField("Content", "what changes in lucene4.", Field.Store.YES));
				list.add(doc);

				writer.addDocuments(list);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (writer != null) {
					try {
						writer.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			
		}
	}
}